package com.me.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.me.constant.Constant;
import com.me.control.CreateWindown;
import com.me.enums.ScreenEnum;
import com.me.happybugs.HappyBugs_Main;
import com.me.helper.PreferencesData;

public class MainMenuScreen extends BaseScreen {

	Image img_bg, img_bgTop;
	static ImageButton img_back;
	Label lb_title;
	TextButton btn_playGame;
	TextButton btn_option;
	TextButton btn_bestScore;
	CreateWindown cr = new CreateWindown();
	Group groupMenu = new Group();
	
	@Override
	public void show() {
		super.show();
		
		// Reset stage
		BaseScreen.stage.clear();
		
		Constant.soundManager.resetAllSound();
		if(PreferencesData.getBoolRememberMusic()){
			Constant.soundManager.playMusic();
		}else{
			Constant.soundManager.resetAllMusic();
		}
		// create the camera and the SpriteBatch
		camera = new OrthographicCamera();
		camera.setToOrtho(false, 1280, 720);
				
		img_bg = new Image(HappyBugs_Main.getAtlas().findRegion("datagame/bg_menuGame"));
		stage.addActor(img_bg);
		
		img_bgTop = new Image(Constant.atlas.findRegion("datagame/menu-bottom-thanh-ngang"));
		img_bgTop.setBounds(0, Constant.STAGE_HEIGHT-70, Constant.STAGE_WIDTH, 70);
		stage.addActor(img_bgTop);
		
		TextureRegionDrawable bup = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/Back_up"));
		TextureRegionDrawable bdown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/Back_down"));
		img_back = new ImageButton(bup, bdown);
		img_back.setX(20);
		img_back.setY(Constant.STAGE_HEIGHT- img_bgTop.getHeight() + (img_bgTop.getHeight() - img_back.getHeight())/2);
		img_back.addListener(new ClickListener(){
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				stage.addActor(CreateWindown.confirmExit());
			}
		});
		stage.addActor(img_back);
		
		lb_title = new Label("CHOOSE YOUR OPTION", Constant.skin,"labelMenu");
		lb_title.setX(Constant.STAGE_WIDTH/2 - lb_title.getWidth()/2);
		lb_title.setY(Constant.STAGE_HEIGHT - img_bgTop.getHeight() + (img_bgTop.getHeight() - lb_title.getHeight())/2);	
		stage.addActor(lb_title);
		
		btn_playGame = new TextButton("Play game", Constant.skin, "textButton_45_bold_sologan");
		btn_playGame.setX(640);
		btn_playGame.setY(450);
		btn_playGame.setWidth(400);
		btn_playGame.setHeight(100);
		btn_playGame.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				HappyBugs_Main.getScreenManager().createScreen(ScreenEnum.PLAY_SCREEN);
				HappyBugs_Main.getScreenManager().getScreen().show();
				Constant.resetGame();
			}
			
		});
		groupMenu.addActor(btn_playGame);
		
		btn_option = new TextButton("Options", Constant.skin, "textButton_45_bold_sologan");
		btn_option.setX(640);
		btn_option.setY(btn_playGame.getY() - 130);
		btn_option.setWidth(400);
		btn_option.setHeight(100);
		btn_option.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				stage.addActor(CreateWindown.Soundoption());
				Constant.b_backDevice = false;
			}
			
		});
		groupMenu.addActor(btn_option);
		
		btn_bestScore = new TextButton("Best score", Constant.skin, "textButton_45_bold_sologan");
		btn_bestScore.setX(640);
		btn_bestScore.setY(btn_option.getY() - 130);
		btn_bestScore.setWidth(400);
		btn_bestScore.setHeight(100);
		btn_bestScore.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.b_bestScoreMain = true;
				HappyBugs_Main.getScreenManager().createScreen(ScreenEnum.HIGH_SCORE_SCREEN);
				HappyBugs_Main.getScreenManager().getScreen().show();
			}
			
		});
		groupMenu.addActor(btn_bestScore);
		
		stage.addActor(groupMenu);
	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		// tell the camera to update its matrices.
		camera.update();

		// tell the SpriteBatch to render in the coordinate system specified by the camera.
		sb.setProjectionMatrix(camera.combined);

		// begin a new batch and draw the bucket and all drops
		sb.begin();
		sb.end();

		if(Gdx.input.isKeyPressed(Keys.BACK)){
			if(Constant.b_backDevice){
				stage.addActor(CreateWindown.confirmExit());
				Constant.b_backDevice = false;
			}
		}
		stage.setViewport(Constant.STAGE_WIDTH, Constant.STAGE_HEIGHT);
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		width = 1280 * width / height;
		height = 720;
		BaseScreen.stage.setViewport(width, height, false);
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}
	
}
